<template>
	<div>
		 <!--配送地址-->
        <div style="height:3.1rem;display:flex;flex-direction: row;"  v-if="!checkObj(addressDefault) && addressDefault.addressId != -1" @click.stop="goToAddress">
            <!-- 箭头 -->
            <div class="chooseAddressBox"></div>
            <div class="aui-list-item-label-icon" style="padding-top:0.5rem;color:#38b035">
                <i class="icon njfont nj-dizhi"></i>
            </div>
            <div style="text-align:left;margin-left:0.3rem;padding:0.3rem 0;width:90%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
                <div style="font-weight:bold;">
                    <span>
                        {{addressDefault && addressDefault.receivername?addressDefault.receivername:addressDefault.phone}}
                    </span>
                     <span style="margin-left:1rem;">
                        {{addressDefault && addressDefault.phone}}
                    </span>
                </div>
                <div style="width:100%;padding-top:0.3rem;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
                    {{addressDetail}}
                </div>
            </div>
        </div>
        <div id="address" @click.stop="goToAddress" style="height:3.1rem;display:flex;justify-content:center;align-items:center" v-if="checkObj(addressDefault) || addressDefault.addressId == -1">
            <div class="noAddress">   
                <div>
                    <div class="addressDel"></div>
                    <div class="addressAdd"></div>
                </div>
                <div style="font-size: 0.8rem;color:#38b035;margin-left:0.25rem">
                    请选择收货地址 
                </div>
            </div>
            <!-- 原来的版本 -->
            <!-- <ul class="aui-list aui-media-list">
                <li class="aui-list-item">
                    <div> -->
                        <!-- <div class="aui-list-item-label-icon">
                            <i class="icon njfont nj-dizhi"></i>
                        </div>
                        <div class="aui-list-item-inner aui-list-item-arrow" v-if="addressDefault && addressDefault.addressid != -1">
                            <div class="aui-list-item-text">
                                <div class="aui-list-item-title" style="width: 64.5%;text-align: left;overflow-x: hidden;white-space: nowrap"><strong>收货人：<span id="co_receiverName">{{addressDefault.receivername?addressDefault.receivername:addressDefault.phone}}</span></strong>
                                	<span class="aui-font-size-14 text_c_gray"></span></div>
                                        
                                <div id="co_receiverPhone" class="aui-list-item-right aui-font-size-14" style="width: 35.5%;text-align: right;margin-left:0; white-space: nowrap">{{addressDefault.phone}}</div>
                            </div>
                            <p style="text-align: left;">
                            	<span class="aui-label aui-label-info"><span id="co_addressLabel">{{getLabel()}}</span></span>
                                    &nbsp;<span id="co_deliveryAddress">{{addressDetail}}</span>
                                <span class="show-address">&nbsp;&nbsp;{{houseNumber}}</span>
                            </p>
                            
                        </div> -->
                        <!-- v-if="!addressDefault || addressDefault.addressid == -1" -->
                        
                        <!-- <div class="aui-list-item-inner aui-list-item-arrow" v-if="!addressDefault || addressDefault.addressid == -1">
                            <div style="text-align: left;color: #EF5423;font-weight: bold;">
                            	请选择收货地址
                            </div>
                        </div> -->
                    <!-- </div>
                </li>
            </ul> -->
        </div>
        <!--配送地址 end-->
	</div>
</template>

<script>
	import axios from 'axios';
	import {mapMutations,mapGetters} from 'vuex';
	export default {
//		getAddressList
		props: {
           getAddressList: {
				type: Object,
				default: function(){
					return {}
				}
			},
			addressItem: {
				type: Object,
				default: function(){
					return {}
				}
			},
			pagepath: {
				type: String,
				default: ""
			},
        },
        computed:{
        	...mapGetters('zx',[
        		"getHomeAddressNow"
        	]),
        	...mapGetters('qxz',[
        		"getLocation"
        	]),
        	...mapGetters('xxy', [
				"getaderssgorder"
			])
        },
        data () {
            return {
                addressDefault:null,
                address:'',
                addressDetail:'',
                houseNumber:''
            }
        },
        methods: {
        	...mapMutations('zx',[
        		"setAddressDetail","setaddFlagMutation","setNowAddressMutation"
        	]),
        	...mapMutations('xxy', [
				"setaderssgorder"
			]),
        	async getArea (code) {
//      		http://172.19.5.89:33001/address/select/1
//				'http://172.19.6.152:33001/address/select/'
//		        let res = await axios.get(window.api.mall_base_host + '/address/getarealist?areaid=' + code)  //erp接口
				let res = await axios.get(window.api.base_host+'/address/select/' + code)
		        return res.data.data
            },
            //判断对象是否为空的方法
            checkObj(obj) {
                for(var key in obj) {
                    return false;
                }
                return true;
            },
		    getName(val,arr) {
		        let result = '';
		        arr && arr.map((item) => {
		            if(item.rid == val){
		                result = item.name;
		            }
		        });
		        return result;
		    },
		    getLabel(){
		    	
		    	if(this.addressDefault.label == 0){
		    		return "家庭";
		    	}else if(this.addressDefault.label == 1){
		    		return "公司";
		    	}else if(this.addressDefault.label == 2){
		    		return "其它"
		    	}else{
		    		return this.addressDefault.label;
		    	}
		    },
        	async getAddress(){
     			
				var myAddress = null;
				if( this.$route.query.name && this.$route.query.name.flag){
        			myAddress = this.$route.query.name;
        		}else{
        			myAddress = this.getLocation;
        		}
        		this.addressDefault = myAddress;
        		if(myAddress && myAddress.addressid != -1){   
        			
        			//可以直接下单
        			let province = await this.getArea(1);
        			let provinceName = this.getName(this.addressDefault.province,province);
        			let city = await this.getArea(this.addressDefault.province);
					let cityName = this.getName(this.addressDefault.city,city);
					let region = await this.getArea(this.addressDefault.city);
					let regionName = this.getName(this.addressDefault.region,region);
					this.address = provinceName + " " + cityName + " " + regionName + this.addressDefault.address;
					this.addressDetail = this.addressDefault.address;
					this.houseNumber = myAddress.houseNumber;
					
					var detail = this.address + "_" + myAddress.houseNumber;
					
					if(myAddress.houseNumber){
						this.setAddressDetail(detail);
					}else{
						this.setAddressDetail(this.address);
					}
					
        		}else if((this.$route.query.name && this.$route.query.name.flag) || (myAddress && myAddress.addressid == -1)){
					//补充地址信息
					// console.log("*&&&&&&&&&*****你在哪进来的")
        			this.address = myAddress.province + " " + myAddress.city + " " + myAddress.region + myAddress.address;
        			this.addressDetail = myAddress.address;
        			this.houseNumber = myAddress.houseNumber;
        			
        			var detail = this.address + "_" + myAddress.houseNumber;
        			if(myAddress.houseNumber){
						this.setAddressDetail(detail);
					}else{
						this.setAddressDetail(this.address);
					}
        		}
        		
        		// 已弃用代码
        		
//      		return ; 
//      		var arr = null;
//      		if(this.getHomeAddressNow && this.getHomeAddressNow != null){
//      			this.addressDefault = this.getHomeAddressNow;
//      			let province = await this.getArea(1);
//      			let provinceName = this.getName(this.addressDefault.province,province);
//      			let city = await this.getArea(this.addressDefault.province);
//					let cityName = this.getName(this.addressDefault.city,city);
//					let region = await this.getArea(this.addressDefault.city);
//					let regionName = this.getName(this.addressDefault.region,region);
//					this.address = provinceName + " " + cityName + " " + regionName + this.addressDefault.address;
//					this.addressDetail = this.addressDefault.address;
//					
//					var detail = this.address + "_" + myAddress.houseNumber;
//      			if(myAddress.houseNumber){
//						this.setAddressDetail(detail);
//					}else{
//						this.setAddressDetail(this.address);
//					}
//					
//      		}else{
//      			//这段代码用于兼容 万一首页没有设置进去就采用默认地址
//					arr = this.getAddressList.addressLists;
//					if(arr != null){
//	        			for(let i = 0; i < arr.length; i++){
//		        			
//		        			if(arr[i].defaultaddress === '1'){
//		        				
//		        				this.addressDefault = arr[i];
//		        				
//								let province = await this.getArea(1);
//								let provinceName = this.getName(this.addressDefault.province,province);
//								let city = await this.getArea(this.addressDefault.province);
//								let cityName = this.getName(this.addressDefault.city,city);
//								let region = await this.getArea(this.addressDefault.city);
//								let regionName = this.getName(this.addressDefault.region,region);
//		
//								this.address = provinceName + " " + cityName + " " + regionName + arr[i].address;
//								
//								this.addressDetail = arr[i].address;
//								
//								var detail = this.address + "_" + myAddress.houseNumber;
//      						if(myAddress.houseNumber){
//      							this.setAddressDetail(detail);
//      						}else{
//      							this.setAddressDetail(this.address);
//      						}
//								
//								console.log(this.addressDefault.province,provinceName,'1')
//								console.log(this.addressDefault.city,cityName,'2')
//								console.log(this.addressDefault.region,regionName,'3')
//		        			}
//		        		}
//	        		}
//      		}
//      		
//				console.log(arr,this.getHomeAddressNow,'home页面')
				
//      		console.log(arr[i].defaultaddress,arr,'出去出去出去');
        		// 已弃用代码
        	},
        	getAddressChoose(){
        		this.addressDefault = this.addressItem;
        		// console.log(this.addressDefault,'地址信息')
        		this.address = this.addressItem.result;
        		this.addressDetail = this.addressItem.address;
        		this.houseNumber = this.addressItem.houseNumber;
        		
        		var detail = this.address + "_" + this.addressDefault.houseNumber;
    			if(this.addressDefault.houseNumber){
					this.setAddressDetail(detail);
				}else{
					this.setAddressDetail(this.address);
				}
        	},
        	initData(){
        		// console.log(this.addressItem,'******************************************选择地址*************************************');
	        	if(this.addressItem && this.addressItem.flag){
	        		// console.log(55)
	        		this.getAddressChoose();
	        	}else{
	        		this.getAddress();
	        	}
        	},
        	goToAddress(){
        		// 控制地址直接去  新增地址页  需求取消
//      		var myAddress = null;
//      		console.log(this.$route.query.name,'路由參數')
//      		if( this.$route.query.name && this.$route.query.name.flag){
//      			myAddress = this.$route.query.name;
//      		}else{
//      			myAddress = this.getLocation;
//      		}
////      		console.log(myAddress,'地址地址');
//				if(myAddress && myAddress.addressid != -1){
//					this.$router.push({ path: 'address',query:{id:1}});
//				}else if(this.getLocation != null){
//					
//					var address = {
//						address:myAddress.address,
//						city:myAddress.city,
//						defaultaddress:"1",
//						label:"0",
//						phone:"",
//						positionlat:myAddress.positionlat,
//						positionlng:myAddress.positionlng,
//						province:myAddress.province,
//						receivername:"",
//						region:myAddress.region,
//						result:"",
//						sex:"0",
//						houseNumber:''
//					}
//					this.setaddFlagMutation(true);
//					this.setNowAddressMutation(address);
//					this.$router.push({ path: 'addAddress',query:{id:1,obj:JSON.stringify(address)}});
//				}else{
//					this.$router.push({ path: 'address',query:{id:1}});
//				}
//      		
//				
//				return 
                if(this.pagepath){
                	this.$router.push({ path: 'address',query:{pagepath:this.pagepath}});
                	this.setaderssgorder(this.getaderssgorder+2)
                }else{
                	this.$router.push({ path: 'address',query:{id:1}});
                }
        		
        	},
        },
        beforeCreate(){
//	        let scriptFile = document.createElement('script')
//	        scriptFile.setAttribute("type","text/javascript")
//	        scriptFile.setAttribute("id","addAddress")
//	        scriptFile.setAttribute("src",'static/script/picker.min.js')
//	        document.getElementsByTagName("head")[0].appendChild(scriptFile)
	    },
	    beforeDestroy(){
//	        let sciDom = document.getElementById('addAddress')
//	        sciDom.parentNode.removeChild(sciDom)
	    },
        created(){
//      	console.log(this.getLocation.addressid,'qxz存入的地址');
			this.initData();
//      	console.log(this.getAddressList,'\\=======')
        }
	}
</script>

<style>
.chooseAddressBox {
    width: .4rem;
    height: .4rem;
    position: absolute;
    top: 1.55rem;
    right: 1.6rem;
    margin-top: -.2rem;
    background: 0 0;
    border: 1px solid #ddd;
    border-top: none;
    border-right: none;
    z-index: 2;
    -webkit-border-radius: 0;
    border-radius: 0;
    -webkit-transform: rotate(-135deg);
    transform: rotate(-135deg);
}
.noAddress {
    display: flex;
    flex-direction:row;
    justify-content:center;
    align-items:center;
    width:8.1rem;
    height:1.7rem;
    border-radius:0.85rem;
    border: 1px solid #38b035;
}
.addressDel{
    height: 0.1rem;
    background-color: #38b035;
    width: 0.7rem;
    margin-top:0.6rem;
    margin-bottom: 0.6rem;
    margin-left:0.3rem;
}
.addressAdd{
    height: 0.1rem;
    background-color: #38b035;
    width: 0.7rem;
    margin-top:0.6rem;
    margin-bottom: 0.6rem;
    margin-left: 0.3rem;
    margin-left: 0.3rem;
    transform: rotate(90deg);
    /* transform: [{rotate:'90deg'}], */
    position: absolute;
    top:0.9rem;
}
</style>